/**CSIT 112
 * Assignment 2: Java Simulator
 *
 *Family Simulator program prompts the user for
 *the selection of parent genes via GUI, then
 *returns a detailed printout in the console window
 *with the randomly-generated genes that have been
 *passed on to a child.
 *
 *ViewSim class handles the user interface for the game
 *
 *@author Miguel Calderon
 *@author Max Manziyenko
 *@version 1.0 Jul 29, 2014
 */

package com.googlecode.familytreesimulator;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.ArrayList;

public class ViewSim implements ActionListener {
	
	/* Frame */
	private JFrame frame;
	
	/* Open access to GeneDatabase*/
	GeneDatabase genes = new GeneDatabase();
	
	/* Buttons */
	JButton opt1 = new JButton(genes.eyeColor.get(0));
	JButton opt2 = new JButton(genes.eyeColor.get(1));
	JButton opt3 = new JButton(genes.eyeColor.get(2));
	JButton opt4 = new JButton(genes.eyeColor.get(3));
	JButton opt5 = new JButton(genes.eyeColor.get(4));
	
	/* Label */
	JLabel label;
	
	/* Count utilized in changeButtons and changeLabel */
	int buttoncount = 0;
	int parentNumber = 1;
	
	/* Temporary storage */
	String tempTrait;
	
	/* Temporary Parent storage */
	ArrayList<String> tempParent = new ArrayList<String>();
	ArrayList<String> tempFather = new ArrayList<String>();
	ArrayList<String> tempMother = new ArrayList<String>();

	
	/* Permanent Parent storage */
	ArrayList<String> motherList = new ArrayList<String>();
	ArrayList<String> fatherList = new ArrayList<String>();
	
	/* Create mother object */
	Parent mother = new Parent("mother");
	/* Create father object */
	Parent father = new Parent("father");
	
	/** main(String[] args) runs program
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		//Create GUI object
		@SuppressWarnings("unused")
		ViewSim gui = new ViewSim();

	}
	
	/**ViewSim starts the GUI
	 * 
	 */
	public ViewSim(){
		
		makeView();
		System.out.println("Mother" + "\n********");
		System.out.print("Eye Color: ");
	}
	
	/** makeView() creates the GUI window
	 * 
	 */
	public void makeView(){
		
		/* Creates JFrame */
		frame = new JFrame("Family Simulator");
		frame.setSize(300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		/* Creates the welcome message */
		label = new JLabel("<html>Welcome to the Family Simulator!<br>"
							+ "Please select the mother's eye color</html>");
		
		/* Adds Action Listeners to buttons */
		opt1.addActionListener(this);
		opt2.addActionListener(this);
		opt3.addActionListener(this);
		opt4.addActionListener(this);
		opt5.addActionListener(this);
		
		/* Creates JPanel and adds labels and buttons */
		JPanel message = new JPanel();
		message.add(label);
		JPanel buttons = new JPanel();
		buttons.setLayout(new FlowLayout());
		buttons.add(opt1);
		buttons.add(opt2);
		buttons.add(opt3);
		buttons.add(opt4);
		buttons.add(opt5);
		frame.add(message, BorderLayout.NORTH);
		frame.add(buttons, BorderLayout.CENTER);
		
		/* Creates Content Pane and sets visibility to true */
		Container contentPane = frame.getContentPane();
		contentPane.add(buttons);
		frame.setVisible(true);
	}

	/** actionPerformed(ActionEvent a) defines the
	 *  results of clicking a button within the GUI
	 * 
	 */
	public void actionPerformed(ActionEvent a){

		if(a.getSource().equals(opt1)){
			tempTrait = opt1.getText();
			System.out.println(tempTrait);
			tempParent.add(buttoncount, tempTrait);
		}
		
		else if(a.getSource().equals(opt2)){
			tempTrait = opt2.getText();
			System.out.println(tempTrait);
			tempParent.add(buttoncount, tempTrait);
		}
		
		else if(a.getSource().equals(opt3)){
			tempTrait = opt3.getText();
			System.out.println(tempTrait);
			tempParent.add(buttoncount, tempTrait);
		}
		
		else if(a.getSource().equals(opt4)){
			tempTrait = opt4.getText();
			System.out.println(tempTrait);
			tempParent.add(buttoncount, tempTrait);
		}
		
		else if(a.getSource().equals(opt5)){
			tempTrait = opt5.getText();
			System.out.println(tempTrait);
			tempParent.add(buttoncount, tempTrait);
		}
		
		changeButtons();
		
		if ((buttoncount == 0) && (parentNumber == 1)){
			storeMotherData();
		}
		
		else if ((buttoncount == 0) && (parentNumber == 2)){
			
			storeFatherData();

			Child child1 = new Child("child");
			System.out.println("\nHere's your new child!" 
							+ "\n***********************");
			child1.importFatherGenes(father.getGenes());
			child1.importMotherGenes(mother.getGenes());
			child1.produceChild(mother.getGenes(),father.getGenes());
			child1.printStats();
			child1.getGenes();
		}
	}
	
	/** changeButtons() updates the buttons
	 *  to be displayed in the GUI
	 * 
	 */
	public void changeButtons()
	{
		if(buttoncount == 0){
			opt1.setText(genes.height.get(0));
			opt2.setText(genes.height.get(1));
			opt3.setText(genes.height.get(2));
			opt4.setVisible(false);
			opt5.setVisible(false);
			
			if(parentNumber == 2)
				label.setText("Please select the father's height");
			else
				label.setText("Please select the mother's height");
			
			System.out.print("Height: ");
		}
		
		else if(buttoncount == 1)
		{
			opt1.setText(genes.hairColor.get(0));
			opt2.setText(genes.hairColor.get(1));
			opt3.setText(genes.hairColor.get(2));
			opt4.setText(genes.hairColor.get(3));
			opt4.setVisible(true);
			
			if(parentNumber == 2)
				label.setText("Please select the father's hair color");
			else
				label.setText("Please select the mother's hair color");
				
			System.out.print("Hair Color: ");

		}
		
		else if(buttoncount == 2)
		{
			opt1.setText(genes.hairType.get(0));
			opt2.setText(genes.hairType.get(1));
			opt3.setText(genes.hairType.get(2));
			opt4.setVisible(false);
			
			if(parentNumber == 2)
				label.setText("Please select the father's hair type");
			else
				label.setText("Please select the mother's hair type");
			
			System.out.print("Hair Type: ");

		}
		
		else if(buttoncount == 3)
		{
			opt1.setText(genes.skinTone.get(0));
			opt2.setText(genes.skinTone.get(1));
			opt3.setText(genes.skinTone.get(2));
			opt4.setText(genes.skinTone.get(3));
			opt4.setVisible(true);
			
			if(parentNumber == 2)
				label.setText("Please select the father's skin tone");
			else
				label.setText("Please select the mother's skin tone");
			
			System.out.print("Skin Tone: ");

		}
		
		else if(buttoncount == 4)
		{
			opt1.setText(genes.eyeColor.get(0));
			opt2.setText(genes.eyeColor.get(1));
			opt3.setText(genes.eyeColor.get(2));
			opt4.setText(genes.eyeColor.get(3));
			opt5.setText(genes.eyeColor.get(4));
			opt5.setVisible(true);
			
			buttoncount = -1;
			
			if(parentNumber == 1)
			{
				label.setText("Please select the father's eye color");
			}
			else if(parentNumber == 2)
			{
				opt1.setVisible(false);
				opt2.setVisible(false);
				opt3.setVisible(false);
				opt4.setVisible(false);
				opt5.setVisible(false);
				label.setText("Child stats are now "
								+ "printed in the console.");
			}	
		}
		
		buttoncount ++;
	}
	
	/** storeMotherData() stores a local copy
	 *  of the mother gene list
	 * 
	 */
	public void storeMotherData()
	{
		tempMother = tempParent;
		mother.setGenes(tempMother);
		
		tempParent = new ArrayList<String>();
		parentNumber ++;
		
		System.out.println("\nFather" + "\n********");
		System.out.print("Eye Color: ");
	}
	
	/** storeFatherData() stores a local copy
	 *  of the father gene list
	 * 
	 */
	public void storeFatherData()
	{
		tempFather = tempParent;
		father.setGenes(tempFather);
	}

	/** getMotherGenes() retrieves the mother genes
	 * 
	 * @return mother gene list
	 */
	public ArrayList<String> getMotherGenes()
	{
		return motherList;
	}
	
	/** getFatherGenes() retrieves the father genes
	 * 
	 * @return father gene list
	 */
	public ArrayList<String> getFatherGenes()
	{
		return fatherList;
	}
}
